new Vue({
    el: '#app',
    data() {
        const Pass_Word = (rule, value, callback) => {
            if (validatePassword(this.form.Pass_Word)) {
              callback(new Error(validatePassword(this.form.Pass_Word)));
            } else {
              callback();
            }
        }
        const User_Number = (rule, value, callback) => {
            if (!validatemobile(this.form.User_Number)) {
              callback(new Error('请输入有效手机号'));
            } else {
              callback();
            }
        }
        
        return {
            form: {
                User_Number:'',
                ID_Name: '',//表名
                Image:'',
                Pass_Word: 'Aa123456',//描述
                Name:'',
                Sex:'0',//0男1女
                Mail:'',
                Creation_Datetime:'',
                Role_SerialNumber:[],
                Row_Status:'0',
                Children:{
                    base_system_user_role:[],
                }
            },
            rules: {
                User_Number: [{ required: true, validator:User_Number, trigger: 'blur' }],
                ID_Name: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
                Pass_Word: [{ required: true, validator:Pass_Word, trigger: 'blur' }],
                Name: [{ required: true, message: '请输入用户名称', trigger: 'blur' }],
                Role_SerialNumber: [{ required: true, message: '请选择角色名称', trigger: 'change' }],
            },
            Status:[{
                label:'是',
                value:'0'
            },{ 
                label:'否',
                value:'2'
            }],
            Role:[],
            loading: false,
        }
    },
    mounted() {
        this.getData();
    },
    methods: {
        getData(){
            const that = this;
            $.ajax({
                headers:requestHeaders(),
                url: `${BASE_URL2}/generalBusiness/generalQueryByTableName`,
                type: 'POST',
                data: {
                    Data: "base_system_role_info",
                    Condition:"Row_Status = '0' and Role_SerialNumber <> 'supplier'"
                },
                success: function (data) {
                    that.Role = data;
                },
                complete: function (XMLHttpRequest) {
                    that.loading = false
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json"
            })
        },
        submitForm(formName) {
            const that = this
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    that.loading = true
                    that.form.Creation_Datetime = getYMDHMSDate();
                    that.form.Role_SerialNumber.forEach(Role_SerialNumber => {
                        if (!that.form.Children.base_system_user_role.some(i => i.Role_SerialNumber == Role_SerialNumber)) {
                            that.form.Children.base_system_user_role.push({
                                System_User_Role_SerialNumber:'',
                                User_Number:'',
                                Role_SerialNumber
                            })
                        }
                    })
                    $.ajax({
                        headers:requestHeaders(),
                        url: `${BASE_URL2}/generalBusiness/generalSave`,
                        type: 'POST',
                        data: {
                            Data: JSON.stringify({ Add: { base_system_user_info: [that.form] } })
                        },
                        success: function (res) {
                            if (res.Status === 'success') {
                                that.$message.success("提交成功")
                                window.history.go(-1)
                            } else {
                                that.$message.error("提交失败，请稍后再试")
                            }
                        },
                        complete: function (XMLHttpRequest) {
                            that.loading = false
                            requestPretreatment(XMLHttpRequest);
                        },
                        dataType: "json"
                    })
                } else {
                    that.$message({
                        message: sumberError,
                        type: 'warning'
                    })
                    return false;
                }
            });
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        },
    }
})